package com.tan.lbs.dao.impl;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;

import com.tan.lbs.dao.MobileUserDAO;
import com.tan.lbs.entity.Designer;
import com.tan.lbs.entity.MobileUser;
import com.tan.lbs.util.HibernateUtil;

public class MobileUserDAOImpl implements MobileUserDAO {

	@Override
	public String create(MobileUser mobileUser) {
		SessionFactory sf = HibernateUtil.getSessionFactory();
		Session s = sf.getCurrentSession();
		s.beginTransaction();
		s.save(mobileUser);
		s.getTransaction().commit();
		return mobileUser.getUserID();
	}

	@Override
	public MobileUser readByID(String userID) {
		SessionFactory sf = HibernateUtil.getSessionFactory();
		Session s = sf.getCurrentSession();
		s.beginTransaction();
		MobileUser d = (MobileUser)s.get(MobileUser.class, userID);
		s.getTransaction().commit();
		return d;
	}

	@Override
	public MobileUser readByUserName(String username) {
		SessionFactory sf = HibernateUtil.getSessionFactory();
		Session s = sf.getCurrentSession();
		s.beginTransaction();
		List<MobileUser> ds = s.createCriteria(MobileUser.class).add(Restrictions.
				eq("username", username)).list();
		s.getTransaction().commit();
		if(ds.size() ==0) return null;
		else {
			return (MobileUser)ds.get(0);
		}
	}

}
